package com.blockmeta.bbs.immodule.adapter;

import androidx.annotation.Nullable;
import android.view.View;
import android.widget.ToggleButton;

import com.blockmeta.bbs.baselibrary.base.adapter.BaseAdapter;
import com.blockmeta.bbs.baselibrary.utils.ToastUtil;
import com.blockmeta.bbs.businesslibrary.util.ColorUtil;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.bean.beaninterface.IMarketAndF10Bean;
import com.chad.library.adapter.base.BaseViewHolder;

import java.text.DecimalFormat;
import java.util.List;

/**
 * @author AirThor
 * @date 2020/7/13.
 */
public class MoreMarketAndF10ShareAdapter extends BaseAdapter<IMarketAndF10Bean, BaseViewHolder> {
    private DecimalFormat mDecimalFormat = new DecimalFormat("0.00");
    private int mSelectedCount;
    private static final int MAX_COUNT = 9;
    private final View mBtnShare;
    public MoreMarketAndF10ShareAdapter(int layoutResId, @Nullable List<IMarketAndF10Bean> data,View btnShare) {
        super(layoutResId, data);
        mBtnShare = btnShare;
    }

    @Override
    protected void convert(BaseViewHolder helper, final IMarketAndF10Bean item) {
        helper.setText(R.id.tv_simple_name,item.getSimpleName());
        helper.setText(R.id.tv_price_rmb,item.getPriceRMB());
        helper.setText(R.id.tv_market_value,item.getMarketValue());
        helper.setText(R.id.tv_price_dollar,item.getPriceDollar());
        helper.setText(R.id.tv_exchange,item.getFullName());
        double increase = item.getIncrease();
        if (increase < 0) {
            helper.setBackgroundRes(R.id.tv_increase, ColorUtil.getDownsBg());
            helper.setText(R.id.tv_increase, mDecimalFormat.format(increase)+"%");
        } else {
            helper.setBackgroundRes(R.id.tv_increase, ColorUtil.getUpsBg());
            helper.setText(R.id.tv_increase, "+" +mDecimalFormat.format(increase)+"%");
        }
        View view = helper.getView(R.id.item_click);
        final ToggleButton toggleButton = helper.getView(R.id.tg_selector);
        toggleButton.setChecked(item.isSelected());
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean selected = !toggleButton.isChecked();
                if (selected) {
                    ++mSelectedCount;
                } else {
                    --mSelectedCount;
                }
                if (mSelectedCount>MAX_COUNT){
                    --mSelectedCount;
                    ToastUtil.show("最多只能选择9个");
                    return;
                }
                item.setSelected(selected);
                toggleButton.setChecked(selected);
                mBtnShare.setEnabled(mSelectedCount>0);
            }
        });
    }

    public void resetSelectedCount(){
        mSelectedCount = 0;
        mBtnShare.setVisibility(View.GONE);
    }
}
